package com.zallds.fabric.controller;

import com.mysql.jdbc.StringUtils;
import com.zallds.fabric.service.impl.myccServiceImpl;
import com.zallds.fabric.service.myccService;
import com.zallds.fabric.util.CommonUtils;
import com.zallds.fabric.util.FabricUtils;
import org.hyperledger.fabric.sdk.Channel;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.TransactionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by liujian on 2018/9/10.
 */
@Controller
@RequestMapping("/fabricApi")
public class TestController {

    @Autowired
    private myccService myccService;

    /**
     * 转账操作
     * @param nameA 转账方
     * @param nameB 到账方
     * @param money 转账金额
     * @return
     */
    @RequestMapping("invoke")
    @ResponseBody
    public String invoke(String nameA,String nameB ,int money){
        try {
            String a = myccService.queryMoney(nameA);
            String b = myccService.queryMoney(nameB);
            if(StringUtils.isNullOrEmpty(b) ||  StringUtils.isNullOrEmpty(a)){
                return "a or b has a problem!!!";
            }else if(Integer.parseInt(a)<money){
                return "a can't invoke !!!";
            }
            return myccService.invoke(nameA,nameB,money);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success!!!";
    }

    /**
     * 查询账户余额
     * @param name 查询人名称
     * @return
     */
    @ResponseBody
    @RequestMapping("queryMycc")
    public String queryMoney(String name) throws Exception {
        String reslt = myccService.queryMoney(name);
        if(StringUtils.isNullOrEmpty(reslt)){
            return "don't have "+name+" !!!";
        }
        return reslt;
    }

}
